#include "air001xx_hal.h"
#include "air001_Core_Board.h"

#include "lcd.h"

DMA_HandleTypeDef HdmaCh1;
SPI_HandleTypeDef Spi1Handle;

static void APP_SystemClockConfig(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /* 振荡器配置 */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; /* 选择振荡器HSE,HSI,LSI,LSE */
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;                          /* 开启HSI */
  RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;                          /* HSI 1分频 */
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_24MHz; /* 配置HSI时钟16MHz */
  RCC_OscInitStruct.HSEState = RCC_HSE_OFF;                         /* 关闭HSE */
  /*RCC_OscInitStruct.HSEFreq = RCC_HSE_16_32MHz;*/
  RCC_OscInitStruct.LSIState = RCC_LSI_OFF;                         /* 关闭LSI */
  RCC_OscInitStruct.LSEState = RCC_LSE_OFF;                         /* 关闭LSE */
  /*RCC_OscInitStruct.LSEDriver = RCC_LSEDRIVE_MEDIUM;*/
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;                      /* 开启PLL */
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;              /* 选择时钟源为HSI */
  /* 配置振荡器 */
  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  /* 时钟源配置 */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1; /* 选择配置时钟 HCLK,SYSCLK,PCLK1 */
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; /* 选择PLL作为系统时钟 */
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;        /* AHB时钟 1分频 */
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;         /* APB时钟 1分频 */
  /* 配置时钟源 */
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
}

void APP_SPIConfig(void)
{
  /*反初始化SPI配置*/
  Spi1Handle.Instance               = SPI1;                       /* SPI1 */
  Spi1Handle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;    /* 4分频 */
  Spi1Handle.Init.Direction         = SPI_DIRECTION_1LINE;       /* 全双工 */
  Spi1Handle.Init.CLKPolarity       = SPI_POLARITY_HIGH;           /* 时钟极性低 */
  Spi1Handle.Init.CLKPhase          = SPI_PHASE_2EDGE ;           /* 数据采样从第一个时钟边沿开始 */
  Spi1Handle.Init.DataSize          = SPI_DATASIZE_8BIT;          /* SPI数据长度为8bit */
  Spi1Handle.Init.FirstBit          = SPI_FIRSTBIT_MSB;           /* 先发送MSB */
  Spi1Handle.Init.NSS               = SPI_NSS_SOFT;        /* NSS软件模式(硬件模式) */
  Spi1Handle.Init.Mode              = SPI_MODE_MASTER;                         /* 配置为主机 */
  HAL_SPI_DeInit(&Spi1Handle);
  /*SPI初始化*/
  HAL_SPI_Init(&Spi1Handle);
}

int main(void)
{
  HAL_Init();
  APP_SystemClockConfig();

  APP_SPIConfig();
  LCD_Init();

  while(1)
  {
    LCD_Fill(0, 0, LCD_W, LCD_H, WHITE);
    LCD_Fill(0, 0, LCD_W, LCD_H, RED);
    LCD_Fill(0, 0, LCD_W, LCD_H, GREEN);
    LCD_Fill(0, 0, LCD_W, LCD_H, BLUE);
    LCD_Fill(0, 0, LCD_W, LCD_H, CYAN);

    LCD_ShowCross(20, 20, RED);
    LCD_ShowCross(20, 152, RED);
    LCD_ShowCross(300, 20, RED);
    LCD_ShowCross(300, 152, RED);

    Draw_Circle(50, 50, 20, BLUE);
    LCD_ShowPicture(160, 95, 40, 40, gImage_1);

    LCD_ShowString(10, 33, (uint8_t*)"LCD_W:", RED, WHITE, 32, 1);
    LCD_ShowIntNum(106, 33, LCD_W, 3, RED, WHITE, 32);
    LCD_ShowString(10, 66, (uint8_t*)"LCD_H:", RED, WHITE, 32, 1);
    LCD_ShowIntNum(106, 66, LCD_H, 3, RED, WHITE, 32);

    LCD_ShowFloatNum1(10, 99, 3.11, 3, RED, WHITE, 32);
    HAL_Delay(1000);
  }
}
